package com.yunji.storage.dao.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.yunji.common.annotation.EventRecord;
import com.yunji.common.base.BaseDaoImpl;
import com.yunji.common.constant.EventBusinessTypeConstant;
import com.yunji.common.utils.bean.BeanUtils;
import com.yunji.storage.bo.DiscardRemoveBO;
import com.yunji.storage.bo.DiscardSelectQueryBO;
import com.yunji.storage.bo.StorageUpdateAuditBO;
import com.yunji.storage.constant.StorageEventTypeConstant;
import com.yunji.storage.dao.IDiscardTicketDao;
import com.yunji.storage.domain.DiscardTicketDO;
import com.yunji.storage.domain.RepairTicketDO;
import com.yunji.storage.enums.DiscardStatus;
import com.yunji.storage.mapper.DiscardTicketMapper;
import org.springframework.stereotype.Repository;

import java.util.Arrays;

@Repository
public class DiscardTicketDaoImpl extends BaseDaoImpl<DiscardTicketMapper, DiscardTicketDO> implements IDiscardTicketDao {
    @Override
    public boolean deleteByTicket(DiscardRemoveBO bo) {
        return remove(new LambdaQueryWrapper<DiscardTicketDO>()
                .eq(DiscardTicketDO::getBelongId, bo.getBelongId())
                .eq(DiscardTicketDO::getDiscardStatus, bo.getDiscardStatus()));
    }

    @Override
    public DiscardTicketDO select(DiscardSelectQueryBO bo) {
        return getOne(new LambdaQueryWrapper<DiscardTicketDO>()
                .eq(DiscardTicketDO::getDiscardCode, bo.getDiscardCode()));
    }

    @Override
    public boolean deleteDraft(Long... discardIds) {
        return remove(new LambdaQueryWrapper<DiscardTicketDO>()
                .in(DiscardTicketDO::getDiscardId, Arrays.asList(discardIds))
                .eq(DiscardTicketDO::getDiscardStatus, DiscardStatus.DRAFT.getCode()));
    }

    @Override
    @EventRecord(businessType = EventBusinessTypeConstant.StorageType.EVENT_STORAGE_DISCARD,eventType = StorageEventTypeConstant.EVENT_TYPE_STORAGE)
    public boolean updateTicket(DiscardTicketDO discardTicket) {
        return updateById(discardTicket);
    }

    @Override
    @EventRecord(businessType = EventBusinessTypeConstant.StorageType.EVENT_STORAGE_DISCARD,eventType = StorageEventTypeConstant.EVENT_TYPE_STORAGE)
    public boolean create(DiscardTicketDO discardTicket) {
        return save(discardTicket);
    }

    @Override
    public void resetAuditStatus(Long discardId) {
        baseMapper.resetAuditStatus(discardId);
    }

    @Override
    @EventRecord(businessType = EventBusinessTypeConstant.StorageType.EVENT_STORAGE_DISCARD,eventType = StorageEventTypeConstant.EVENT_TYPE_AUDIT)
    public boolean updateAuditStatus(StorageUpdateAuditBO auditBO) {
        return update(new LambdaUpdateWrapper<DiscardTicketDO>()
                .set(BeanUtils.isNotNull(auditBO.getAuditStatus()), DiscardTicketDO::getAuditStatus,auditBO.getAuditStatus())
                .set(BeanUtils.isNotNull(auditBO.getAuditId()),DiscardTicketDO::getAuditId,auditBO.getAuditId())
                .set(BeanUtils.isNotNull(auditBO.getAuditName()),DiscardTicketDO::getAuditName,auditBO.getAuditName())
                .set(BeanUtils.isNotNull(auditBO.getAuditBelongId()),DiscardTicketDO::getAuditBelongId,auditBO.getAuditBelongId())
                .set(BeanUtils.isNotNull(auditBO.getAuditBelongName()),DiscardTicketDO::getAuditBelongName,auditBO.getAuditBelongName())
                .set(BeanUtils.isNotNull(auditBO.getAuditTime()),DiscardTicketDO::getAuditTime,auditBO.getAuditTime())
                .eq(DiscardTicketDO::getDiscardId, auditBO.getBusinessId()));
    }

}
